package com.sfc.spiderweb.rest;

import com.sfc.spiderweb.service.LazadaActivityService;
import com.sfc.spiderweb.service.LazadaUserService;
import com.sfc.spiderweb.service.PlatformRdpUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 李振伟(Earl Li)
 * @title: LazadaActivityIdTask
 * @projectName spider-web
 * @description: ${description}
 * @date 2019/12/1916:17
 */
@Api(tags = "爬虫发送邮件接口")
@RestController
@RequestMapping(value = "/spider")
@Slf4j
public class LazadaActivityIdTask {

    @Resource
    private LazadaActivityService lazadaActivityService;

    @Resource
    private LazadaUserService lazadaUserService;

    @Resource
    private PlatformRdpUserService platformRdpUserService;

    @ApiOperation(notes = "爬虫定时任务接口", value = "增加一条Action数据")
    @GetMapping("/command")
    public void spiderTask(@ApiParam(value = "spiderHandlers", required = true) String spiderHandlers) {
        if (StringUtils.isEmpty(spiderHandlers)) {
            log.info("没有参数信息");
            throw new RuntimeException("没有参数信息");
        }
        lazadaActivityService.lazadaActivityTask(spiderHandlers);
    }

    @ApiOperation(notes = "lazada周表现发送邮件", value = "")
    @GetMapping("/lazadaPerformance")
    public void lazadaPerformance() {
        log.info("lazada周表现发送邮件开始");
        lazadaUserService.lazadaPerformance();
        log.info("lazada周表现发送邮件结束");
    }

    @ApiOperation(notes = "amazon日转款发送邮件", value = "")
    @GetMapping("/amazonTransferSendMail")
    public void amazonTransferSendMail() {
        log.info("amazonTransferSendMail开始");
        try {
            platformRdpUserService.sendTransferMail();
        } catch (Exception e) {
            e.printStackTrace();
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            HttpServletResponse resp = ((ServletRequestAttributes) requestAttributes).getResponse();
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            log.info("amazonTransferSendMail结束");
        }
    }

}
